//
//  MainViewController.m
//  FourthProject
//
//  Created by mac on 16/5/25.
//  Copyright © 2016年 cry. All rights reserved.
//

#import "MainViewController.h"
#import "RecommendViewController.h"
#import "DestinationViewController.h"
#import "CommunityViewController.h"
#import "SearchViewController.h"
#import "LoginViewController.h"

@interface MainViewController ()<UIScrollViewDelegate>
{
     UIImageView               *passView;
}
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBarHidden = YES;
    
    self.loginButton.layer.cornerRadius = 20;
    self.loginButton.clipsToBounds = YES;
    
    self.searchButton.layer.cornerRadius = 20;
    self.searchButton.clipsToBounds = YES;
    
    _myScroll.contentSize = CGSizeMake(WIDTH * 3, HEIGHT - 66);
    
    RecommendViewController *recommendVC = [[RecommendViewController alloc] init];
    DestinationViewController *destinationVC = [[DestinationViewController alloc] init];
    CommunityViewController *communityVC = [[CommunityViewController alloc] init];
    
    recommendVC.view.frame = CGRectMake(5, -10, _myScroll.frame.size.width - 10, _myScroll.frame.size.height);
    recommendVC.controller = self;
    
    destinationVC.view.frame = CGRectMake(WIDTH, 0, _myScroll.frame.size.width, _myScroll.frame.size.height);
    communityVC.view.frame = CGRectMake(WIDTH * 2, 0, _myScroll.frame.size.width, _myScroll.frame.size.height);
    
    [_myScroll addSubview:recommendVC.view];
    [_myScroll addSubview:destinationVC.view];
    [_myScroll addSubview:communityVC.view];
    
    passView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    passView.image = [UIImage imageNamed:@"LaunchImage-700-568h@2x"];
    [self.view addSubview:passView];
    
    [self performSelector:@selector(openMachineAnimation) withObject:nil afterDelay:0.5];
    
    
}

- (void)openMachineAnimation {
    [UIView animateWithDuration:2 animations:^{
        passView.frame = CGRectMake(-WIDTH, 0, WIDTH, HEIGHT);
    } completion:^(BOOL finished) {
        ;
    }];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    int m = scrollView.contentOffset.x / WIDTH;

    UIButton *button = [self.view viewWithTag:10 + m];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.moveView.center = CGPointMake(button.center.x, self.moveView.center.y);
    }];
}

- (IBAction)threeButtonClick:(id)sender {
    UIButton *button = (UIButton *)sender;
    
    [UIView animateWithDuration:0.5 animations:^{
        self.moveView.center = CGPointMake(button.center.x, self.moveView.center.y);
    }];
    
    _myScroll.contentOffset = CGPointMake(WIDTH * (button.tag - 10), 0);
    
}

- (IBAction)searchButtonClick:(id)sender {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSDictionary *dic = @{@"client_id":@"qyer_android",@"client_secret":@"9fcaae8aefc4f9ac4915",@"v":@"1",@"track_deviceid":@"869070029694844",@"track_app_version":@"6.9.0",@"track_app_channel":@"xiaomi",@"track_device_info":@"lte26007",@"track_os":@"Android4.4.4",@"app_installtime":@"1463542775388"};
    
    [manager POST:@"http://open.qyer.com/qyer/search/hot_history" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"====%@",responseObject);
        SearchViewController *searchVC = [[SearchViewController alloc] init];
        searchVC.recommendArray = responseObject[@"data"];
        [self presentViewController:searchVC animated:YES completion:nil];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"!!!!!%@",error);
    }];
    
    //目的地
    //http://open.qyer.com/qyer/search/index?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=869070029694844&track_app_version=6.9.0&track_app_channel=xiaomi&track_device_info=lte26007&track_os=Android4.4.4&app_installtime=1463542775388&type=place&keyword=%E5%86%B2%E7%BB%B3&count=20&page=1
    
    
    //帖子
    //http://open.qyer.com/qyer/search/index?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=869070029694844&track_app_version=6.9.0&track_app_channel=xiaomi&track_device_info=lte26007&track_os=Android4.4.4&app_installtime=1463542775388&type=thread&keyword=%E5%86%B2%E7%BB%B3&count=20&page=1
    
    //折扣
    //http://open.qyer.com/lastminute/app_lastminute_list
    //@"page":@"1",@"pagesize":@"20",@"product_type":@"0",@"continent_id":@"0",@"country_id":@"0",@"is_show_pay":@"1",@"is_show_supplier":@"1",@"kw":@"冲绳"
    
    
    //用户
    //http://open.qyer.com/qyer/search/index?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=869070029694844&track_app_version=6.9.0&track_app_channel=xiaomi&track_device_info=lte26007&track_os=Android4.4.4&app_installtime=1463542775388&type=user&keyword=%E5%86%B2%E7%BB%B3&count=20&page=1
    
}

- (IBAction)loginButtonClick:(id)sender {
    
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    
    [self.navigationController pushViewController:loginVC animated:YES];
    
    
    //account_s bd3c27330d6b0769e991e2ed19447107
    //grant_type password
    //account_type 1
    //username   943081641@qq.com
    //password   wb943081641
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSDictionary *dic = @{@"client_id":@"qyer_android",@"client_secret":@"9fcaae8aefc4f9ac4915",@"v":@"1",@"track_deviceid":@"869070029694844",@"track_app_version":@"6.9.0",@"track_app_channel":@"xiaomi",@"track_device_info":@"lte26007",@"track_os":@"Android4.4.4",@"app_installtime":@"1463542775388",@"account_s":@"bd3c27330d6b0769e991e2ed19447107",@"grant_type":@"password",@"account_type":@"1",@"username":@"943081641@qq.com",@"password":@"wb943081641"};
    
    [manager POST:@"http://open.qyer.com/qyer/user/login" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"====%@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"!!!!!%@",error);
    }];

    
}




@end
